local qiefu = fk.CreateSkill{
  name = "hanqing__qiefu",
}

Fk:loadTranslationTable{
  ["hanqing__qiefu"] = "窃符",
  [":hanqing__qiefu"] = "准备阶段，你可以令两名角色进行<a href='delayedPindian'>延时拼点</a>，然后令其中一名角色执行你本回合的后续阶段，"..
  "其造成伤害时，公布拼点结果，赢的角色移动场上一张牌。拼点牌进入弃牌堆前，参与拼点角色使用伤害牌无距离限制。",

  ["#hanqing__qiefu-choose"] = "窃符：令两名角色进行延时拼点，然后令其中一名角色替你执行本回合后续阶段",
  ["#hanqing__qiefu2-choose"] = "窃符：令其中一名角色替你执行本回合后续阶段",
  ["@@hanqing__qiefu-turn"] = "窃符",
  ["#hanqing__qiefu-move"] = "窃符：请移动场上一张牌",
}

local U = require "packages/utility/utility"

qiefu:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(qiefu.name) and player.phase == Player.Start and
      table.find(player.room.alive_players, function (p)
        return table.find(player.room:getOtherPlayers(p, false), function (q)
          return p:canPindian(q)
        end) ~= nil
    end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "#hanqing__qiefu_active",
      prompt = "#hanqing__qiefu-choose",
      cancelable = true,
      no_indicate = false,
    })
    if success and dat then
      event:setCostData(self, {tos = dat.targets})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos
    room:addPlayerMark(targets[1], "hanqing__qiefu_use-turn", 1)
    room:addPlayerMark(targets[2], "hanqing__qiefu_use-turn", 1)
    local pindian = U.delayedPindian(targets[1], {targets[2]}, qiefu.name)
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    if turn_event == nil then return end
    turn_event:addCleaner(function()
      U.delayedPindianCleaner(pindian)
    end)
    if player.dead or (targets[1].dead and targets[2].dead) then return end
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = qiefu.name,
      prompt = "#hanqing__qiefu2-choose",
      cancelable = false,
    })[1]
    local phase_table = {}
    local phase_data = nil
    for i = turn_event.data.phase_index + 1, #turn_event.data.phase_table, 1 do
      phase_data = turn_event.data.phase_table[i]
      if not phase_data.skipped then
        table.insert(phase_table, phase_data.phase)
        phase_data.skipped = true
      end
    end
    room:setPlayerMark(to, "@@hanqing__qiefu-turn", 1)
    room:setPlayerMark(to, "hanqing__qiefu-turn", phase_table)
  end,
})

qiefu:addEffect(fk.EventPhaseSkipped, {
  can_refresh = function (self, event, target, player, data)
    return player:getMark("hanqing__qiefu-turn") ~= 0 and
      data.phase <= Player.Finish and data.phase >= Player.Start
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("hanqing__qiefu-turn")
    local phase = mark[1]
    table.remove(mark, 1)
    room:setPlayerMark(player, "hanqing__qiefu-turn", #mark > 0 and mark or 0)
    player:gainAnExtraPhase(phase, "game_rule", false)
  end,
})

qiefu:addEffect(fk.DamageCaused, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("@@hanqing__qiefu-turn") ~= 0 and
      player:usedEffectTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local pindian  ---@type Utility.DelayedPindianData
    room.logic:getEventsOfScope(GameEvent.DelayedPindian, 1, function(e)
      local dat = e.data
      if dat.reason == qiefu.name and (dat.from == player or table.contains(dat.tos, player)) then
        pindian = dat
        return true
      end
    end, Player.HistoryTurn)
    if pindian == nil then return end
    local from, to = pindian.from, pindian.tos[1]
    room:removePlayerMark(from, "hanqing__qiefu_use-turn", 1)
    room:removePlayerMark(to, "hanqing__qiefu_use-turn", 1)
    pindian = U.delayedPindianDisplay(pindian)
    U.delayedPindianCleaner(pindian)
    local winner = pindian.results[to].winner
    if winner and not winner.dead and #room:canMoveCardInBoard() > 0 then
      local targets = room:askToChooseToMoveCardInBoard(winner, {
        prompt = "#hanqing__qiefu-move",
        skill_name = qiefu.name,
        cancelable = false,
      })
      room:askToMoveCardInBoard(winner, {
        target_one = targets[1],
        target_two = targets[2],
        skill_name = qiefu.name,
      })
    end
  end,
})

qiefu:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and card.is_damage_card and player:getMark("hanqing__qiefu_use-turn") > 0
  end,
})

return qiefu
